package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nongjt.domain.NjtCertificatePrint;
import com.ruoyi.nongjt.service.NjtCertificatePrintService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/nongjt/certificatePrint")
public class NjtCertificatePrintController extends BaseController {
    @Autowired
    private NjtCertificatePrintService njtCertificatePrintService;

    //    有条件的分页查询
    @GetMapping("/list")
    public TableDataInfo list(NjtCertificatePrint njtCertificatePrint) {
        startPage();
        List<NjtCertificatePrint> list = njtCertificatePrintService.getNjtCertificatePrintList(njtCertificatePrint);
        return getDataTable(list);
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody NjtCertificatePrint njtCertificatePrint) {
        int rows = njtCertificatePrintService.updateNjtCertificatePrint(njtCertificatePrint);
//        if (rows > 0) {
//            return AjaxResult.success("修改成功");
//        } else {
//            return AjaxResult.error("修改失败");
//        }
//        若依官方封装一个方法用户判断受影响行数，可以直接继承BaseController，然后调用
        return toAjax(rows);
    }

    @DeleteMapping("/delete") // 路径中不包含占位符
    public AjaxResult deleteBatch(@RequestBody NjtCertificatePrint njtCertificatePrint) {
        if (njtCertificatePrint == null || njtCertificatePrint.getIds() == null || njtCertificatePrint.getIds().isEmpty()) {
            return AjaxResult.error("删除失败，未提供有效的ID列表");
        }
        // 调用Service层方法执行删除
        int affectedRows = njtCertificatePrintService.deleteNjtCertificatePrintByIds(njtCertificatePrint.getIds());
        if (affectedRows > 0) {
            return AjaxResult.success("删除成功，共删除 " + affectedRows + " 条记录");
        }
        return AjaxResult.error("删除失败，未找到对应记录或删除过程出错");
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, NjtCertificatePrint njtCertificatePrint) {
        List<NjtCertificatePrint> list = njtCertificatePrintService.getNjtCertificatePrintList(njtCertificatePrint);
        ExcelUtil<NjtCertificatePrint> util = new ExcelUtil<>(NjtCertificatePrint.class);
        util.exportExcel(response, list, "合格证打印列表数据");
    }
}
